//注册页
$('.register').on('click', function () {
   /*  location.href = 'register.html' */
   location.href='../register.html'
})
//首页
$('.index').on('click', function () {
    location.href = '../index.html'
})
$('form').on('submit', function () {
    var username = $('[name="username"]').val()
    var password = $('[name="password"]').val()
    //表单校验
    var reg1 = /^[a-z0-9]\w{4,11}$/;
    if (!reg1.test(username)) {
        layer.msg('用户名不规范', {
            icon: 2,
            time: 1000
        })
        return false
    }
    var reg2 = /\w{6,12}/;
    if (!reg2.test(password)) {
        layer.msg('密码不规范', {
            icon: 2,
            time: 1000
        })
        return false
    }
    //勾选同意协议
    if (($('.gouxuan').children().first().prop('checked'))) {
        //勾选了
        var xhr = new XMLHttpRequest;
        xhr.open('post', '/api/login');
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
        xhr.send(`username=${username}&password=${password}`)
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    var res = xhr.responseText;
                    res = JSON.parse(res);
                    console.log(res);
                    if (res.code === 1) {   //登录成功
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1000
                        }, () => {
                            window.localStorage.setItem('userInfo', res.userInfo)
                            window.localStorage.setItem('name', username)
                            let url = window.localStorage.getItem('url')
                            if(!url){  //url存在
                                location.href = '../index.html'
                            }else{
                                location.href = url
                                window.localStorage.removeItem('url')
                            }
                        })
                    } else if (res.code === 2) { //登录失败，用户名或密码错误
                        layer.msg(res.msg, {
                            icon: 2,
                            time: 1000
                        }, () => {
                            $('[name="username"]').val('')
                            $('[name="password"]').val('')
                            $('[type="radio"]').prop('checked',false)
                        })
                        return false
                    } else {  //用户名不存在
                        layer.msg(res.msg, {
                            icon: 2,
                            time: 1000
                        }, () => {
                            location.href = '../register.html'
                        })
                        return false
                    }
                }
            }
        }
    } else {
        layer.msg('请勾选服务协议', {
            icon: 2,
            time: 1000
        }, () => {
            return false
        })
    }

    //阻止默认行为
    return false;
})